package com.hxj.ecokey.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxj.ecokey.entity.UserPoints;
import com.hxj.ecokey.mapper.UserPointsMapper;
import com.hxj.ecokey.service.UserPointsService;
import org.springframework.stereotype.Service;

@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints> implements UserPointsService {
    @Override
    public int getUserPoints(Integer userId){
        UserPoints userPoints = this.lambdaQuery().eq(UserPoints::getUserId,userId).one();
        return userPoints != null ? userPoints.getPoints() : 0;
    }

    @Override
    public boolean updateUserPoints(Integer userId,Integer points){
        UserPoints userPoints = this.lambdaQuery().eq(UserPoints::getUserId,userId).one();

        if (userPoints == null){
            userPoints = new UserPoints();
            userPoints.setUserId(userId);
            userPoints.setPoints(points);
            return this.save(userPoints);
        }else {
            userPoints.setPoints(points);
            return this.updateById(userPoints);
        }
    }
}
